<?php

/**
* @file
* Contains \Drupal\campusapp\Form\SettingsForm.
*/

namespace Drupal\campusapp\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
* Configure URL and key of campusapp
*/
class SettingsForm extends ConfigFormBase {
	/**
	* {@inheritdoc}
	*/
	public function getFormId() {
		return 'campusapp_settings';
	}
	
	/**
	* {@inheritdoc}
	*/
	protected function getEditableConfigNames() {
		return ['campusapp.settings'];
	}
	
	/**
	* {@inheritdoc}
	*/
	public function buildForm(array $form, FormStateInterface $form_state) {
		$settings=$this->config('campusapp.settings');
		$form=[];
		$form['host']=array(
			'#type'=>'textfield',
			'#title'=>$this->t('Campusapp Server host name'),
			'#default_value' => empty($settings->get('host'))?'':$settings->get('host'),
			'#field_prefix' => 'https://',
			'#field_suffix' => '/api',
			'#required' => TRUE,
		);
		$form['appid']=array(
			'#type' => 'textfield',
			'#title' => $this->t('APP ID'),
			'#default_value' => empty($settings->get('appid'))?'':$settings->get('appid'),
			'#required' => TRUE,
		);
		$form['appsecret']=array(
			'#type' => 'textfield',
			'#title' => $this->t('APP Secret'),
			'#default_value' => empty($settings->get('appsecret'))?'':$settings->get('appsecret'),
			'#required' => TRUE,
		);

		$form['integration']=array(
			'#type' => 'details',
			'#title' => $this->t('User Login Integration'),
			'#open' => TRUE,
		);
		$form['integration']['loginform']=array(
			'#type' => 'radios',
			'#title' => $this->t('User Login Form Altering'),
			'#options' => [
				'0' => $this->t('Do not alter'),
				'1' => $this->t('Replace traditional login form completely'),
				'2' => $this->t('Display as a side widget'),
			],
			'#required' => TRUE,
			'#default_value' => $settings->get('loginform'),
		);
		$form['integration']['autocreation']=array(
			'#type' => 'checkbox',
			'#title' => $this->t('Automatically create new user if not exists.'),
			'#prefix' => campusapp_getSign($data, $settings->get('apikey')),
			'#default_value' => $settings->get('autocreation'),
		);
		return parent::buildForm($form, $form_state);
	}

	/**
	* {@inheritdoc}
	*/
	public function validateForm(array &$form, FormStateInterface $form_state) {
		$url=sprintf('https://%s/api/third/get-token', $form_state->getValue('host'));
		$data=['appid'=>$form_state->getValue('appid'),'appsecret'=>$form_state->getValue('appsecret')];
		$response = \Drupal::httpClient()->get($url, ['query' => $data]);

		if($response->getStatusCode()!='200')
			$form_state->setErrorByName('host', $response->getStatusCode().': '.$response->getReasonPhrase());
		else {
			$data=json_decode((string)$response->getBody());
			$access_token=$data->d->access_token;
			if(!$access_token)
				$form_state->setErrorByName('appid', t("APPID and APPSECRET may be mismatched."));
			else
				\Drupal::cache()->set('campusapp.access_token', $access_token, time()+$data->d->expires_in);
		}
	}

	/**
	* {@inheritdoc}
	*/
	public function submitForm(array &$form, FormStateInterface $form_state) {
		$this->config('campusapp.settings')
			->set('host', $form_state->getValue('host'))
			->set('appid', $form_state->getValue('appid'))
			->set('appsecret', $form_state->getValue('appsecret'))
			->set('loginform', $form_state->getValue('loginform'))
			->set('autocreation', $form_state->getValue('autocreation'))
			->save();
		
		parent::submitForm($form, $form_state);
	}
}
?>

